package thread;

import java.util.concurrent.Semaphore;

/**
 * Created with IntelliJ IDEA.
 * Description: semaphore实现资源分配
 * User: 王圆豪
 * Date: 2025-05-07
 * Time: 23:30
 */
public class Demo37 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);
        Thread t1 = new Thread(()->{
            try {
                semaphore.acquire();
                semaphore.release();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t2 = new Thread(()->{
            try {
                semaphore.acquire();
                semaphore.release();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread t3 = new Thread(()->{
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }
}
